/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Desktop_RecoverPassword.java
 *
 * Created on Apr 7, 2011, 11:15:15 AM
 */
package DesktopGUIpackage;

import Controller.DesktopController;
import Controller.DesktopControllerHelper;
import DTO.Customer;
import DTO.Email;
import EmailService.EmailSender;
import Utilities.EmailTemplates;
import Utilities.SimpleProtector;

/**
 *
 * @author edaype
 */
public class Desktop_RecoverPassword extends javax.swing.JPanel {

    private DesktopController desktopController;
    private DesktopControllerHelper desktopControllerHelper;

    /** Creates new form Desktop_RecoverPassword */
    public Desktop_RecoverPassword(DesktopControllerHelper desktopControllerHelper) {
        initComponents();
        this.desktopControllerHelper = desktopControllerHelper;
        desktopController = desktopControllerHelper.getDesktopController();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        TitlePanel = new javax.swing.JPanel();
        RecoverButton = new javax.swing.JButton();
        TitleLabel = new javax.swing.JLabel();
        MainPanel = new javax.swing.JPanel();
        ItemPanel = new javax.swing.JPanel();
        EmailLabel = new javax.swing.JLabel();
        EmailField = new javax.swing.JTextField();
        StoreCardField = new javax.swing.JTextField();
        StoreCardLabel = new javax.swing.JLabel();
        ErrorLabel = new javax.swing.JLabel();
        InstructionPanel = new javax.swing.JPanel();
        InstructionLabel2 = new javax.swing.JLabel();
        InstructionLabel1 = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        TitlePanel.setBackground(new java.awt.Color(255, 255, 255));

        RecoverButton.setFont(new java.awt.Font("Tahoma", 0, 18));
        RecoverButton.setText("Recover");
        RecoverButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RecoverButtonActionPerformed(evt);
            }
        });

        TitleLabel.setFont(new java.awt.Font("Copperplate Gothic Bold", 2, 45));
        TitleLabel.setText("Password Recovery");

        javax.swing.GroupLayout TitlePanelLayout = new javax.swing.GroupLayout(TitlePanel);
        TitlePanel.setLayout(TitlePanelLayout);
        TitlePanelLayout.setHorizontalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(TitleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 543, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 92, Short.MAX_VALUE)
                .addComponent(RecoverButton, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        TitlePanelLayout.setVerticalGroup(
            TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(TitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(TitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(TitleLabel)
                    .addComponent(RecoverButton, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        MainPanel.setBackground(new java.awt.Color(255, 255, 255));

        ItemPanel.setBackground(new java.awt.Color(255, 255, 255));
        ItemPanel.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));

        EmailLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        EmailLabel.setText("Email");

        EmailField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StoreCardField.setFont(new java.awt.Font("Tahoma", 0, 24));

        StoreCardLabel.setFont(new java.awt.Font("Tahoma", 0, 24));
        StoreCardLabel.setText("StoreCard Number");

        ErrorLabel.setFont(new java.awt.Font("Tahoma", 0, 12));
        ErrorLabel.setText("Error Label / Confirmation Label");

        javax.swing.GroupLayout ItemPanelLayout = new javax.swing.GroupLayout(ItemPanel);
        ItemPanel.setLayout(ItemPanelLayout);
        ItemPanelLayout.setHorizontalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(ItemPanelLayout.createSequentialGroup()
                        .addGap(164, 164, 164)
                        .addComponent(ErrorLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 347, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(ItemPanelLayout.createSequentialGroup()
                        .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(StoreCardLabel)
                            .addComponent(EmailLabel))
                        .addGap(18, 18, 18)
                        .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(StoreCardField)
                            .addComponent(EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        ItemPanelLayout.setVerticalGroup(
            ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(ItemPanelLayout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(EmailLabel)
                    .addComponent(EmailField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(ItemPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(StoreCardLabel)
                    .addComponent(StoreCardField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(ErrorLabel)
                .addContainerGap(34, Short.MAX_VALUE))
        );

        InstructionPanel.setBackground(new java.awt.Color(255, 255, 255));

        InstructionLabel2.setFont(new java.awt.Font("Tahoma", 0, 24));
        InstructionLabel2.setText("please specify the following information");

        InstructionLabel1.setFont(new java.awt.Font("Tahoma", 0, 24));
        InstructionLabel1.setText("In order to retreive your password");

        javax.swing.GroupLayout InstructionPanelLayout = new javax.swing.GroupLayout(InstructionPanel);
        InstructionPanel.setLayout(InstructionPanelLayout);
        InstructionPanelLayout.setHorizontalGroup(
            InstructionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InstructionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(InstructionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(InstructionLabel2)
                    .addComponent(InstructionLabel1))
                .addContainerGap())
        );
        InstructionPanelLayout.setVerticalGroup(
            InstructionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InstructionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(InstructionLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(InstructionLabel2)
                .addContainerGap())
        );

        javax.swing.GroupLayout MainPanelLayout = new javax.swing.GroupLayout(MainPanel);
        MainPanel.setLayout(MainPanelLayout);
        MainPanelLayout.setHorizontalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap(122, Short.MAX_VALUE)
                .addGroup(MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(InstructionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(ItemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(125, 125, 125))
        );
        MainPanelLayout.setVerticalGroup(
            MainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(MainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(InstructionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(68, 68, 68)
                .addComponent(ItemPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(MainPanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(TitlePanel, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(MainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                .addComponent(TitlePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void RecoverButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RecoverButtonActionPerformed
        // TODO add your handling code here:
        String storecard = StoreCardField.getText();
        String email = EmailField.getText();
        Customer customer = new Customer();
        Email emailVO = new Email();
        boolean isValid = false;
        isValid = validateFields();
        if(isValid){

             try {
            customer = desktopController.getCustomerInfo(Integer.parseInt(storecard));

            if (email.equalsIgnoreCase(customer.getEmail())) {

                String[] to = {email};
                emailVO.setFrom(EmailTemplates.adminEmail);
                emailVO.setTo(to);
                emailVO.setEmailSubjectTxt(EmailTemplates.PASSWORD_RECOVERY_SUBJECT);
                emailVO.setEmailMsgTxt(EmailTemplates.PASSWORD_RECOVERY_BODY_1
                        + " " + customer.getFirstName() + ", "
                        + EmailTemplates.PASSWORD_RECOVERY_BODY_2 + " "
                        + SimpleProtector.decrypt(customer.getPassCode()));
                EmailSender.sendEmail(emailVO);
                desktopController.setMessage("dear " + customer.getFirstName()
                        + ", \n\t password is emailed to you.");
                desktopControllerHelper.changeFrame(5);
            } else {
                ErrorLabel.setText("email and store card donot match");
            }
        } catch (Exception ex) {
                ErrorLabel.setText(ex.getMessage());
            }

        }

}//GEN-LAST:event_RecoverButtonActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField EmailField;
    private javax.swing.JLabel EmailLabel;
    private javax.swing.JLabel ErrorLabel;
    private javax.swing.JLabel InstructionLabel1;
    private javax.swing.JLabel InstructionLabel2;
    private javax.swing.JPanel InstructionPanel;
    private javax.swing.JPanel ItemPanel;
    private javax.swing.JPanel MainPanel;
    private javax.swing.JButton RecoverButton;
    private javax.swing.JTextField StoreCardField;
    private javax.swing.JLabel StoreCardLabel;
    private javax.swing.JLabel TitleLabel;
    private javax.swing.JPanel TitlePanel;
    // End of variables declaration//GEN-END:variables

    private boolean validateFields() {
        
            boolean isValid = true;

            if("".equalsIgnoreCase(StoreCardField.getText().trim())){
                isValid = false;
                ErrorLabel.setText(" store card is mandatory");
                //return isValid;
                
            }else if("".equalsIgnoreCase(EmailField.getText().trim())){

                isValid = false;
                ErrorLabel.setText("email is mandatory");
               // return isValid;
                
            }else{
                 isValid = ValidateEmail();
            }

            return isValid;

    }

     private boolean ValidateEmail() {
         
        char[] temp1 = EmailField.getText().toCharArray();
        String temp2 = EmailField.getText().toString();
        int count = 0;
        int size = temp1.length;
        int ascii1 = 0;
        int ascii2 = 0;
        int ascii3 = 0;
        int ascii4 = 0;
        int LastDot = temp2.lastIndexOf(".");
        //Determine if ends something like .com or .ca
        int diff = size - LastDot;


        try {
            ascii1 = EmailField.getText().toLowerCase().charAt(size - 1);
            ascii2 = EmailField.getText().toLowerCase().charAt(size - 2);
            ascii3 = EmailField.getText().toLowerCase().charAt(size - 3);
            ascii4 = EmailField.getText().toLowerCase().charAt(size - 4);

        } catch (StringIndexOutOfBoundsException siobe) {
            return false;
        }

        for (int i = 0; i < temp1.length; i++) {
            if (temp1[i] == '@') {
                count += 1;
            }
        }
        if (diff == 3) {
            if (ascii1 < 97 || ascii1 > 122) {
                ErrorLabel.setText("Invalid Email Address");
                return false;
            } else if (ascii2 < 97 || ascii2 > 122) {
                ErrorLabel.setText("Invalid Email Address");
                return false;
            }
        } else if (diff == 4) {
            if (ascii1 < 97 || ascii1 > 122) {
                ErrorLabel.setText("Invalid Email Address");
                return false;
            } else if (ascii2 < 97 || ascii2 > 122) {
                ErrorLabel.setText("Invalid Email Address");
                return false;
            } else if (ascii3 < 97 || ascii3 > 122) {
                ErrorLabel.setText("Invalid Email Address");
                return false;
            }
        } else {
            ErrorLabel.setText("Invalid Email Address");
            return false;
        }
        if (count != 1) {
            ErrorLabel.setText("Invalid Email Address");
            return false;
        }
        return true;
    }
}

